 package com.headcaselabs.orchestra;



import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.headcaselabs.queue.SQSUtils;
import com.xerox.amazonws.sqs.Message;

public class MessageControl {
	public static final int CLEANUP_TIMEOUT = 30;
	
	private Configuration conf;

  private final static Log log_ =
    LogFactory.getLog(MessageControl.class.getName());
	
	private SQSUtils sqsUtils;
	public MessageControl(SQSUtils sqsUtils, Configuration conf) {
		this.conf = conf;
		this.sqsUtils = sqsUtils;
	}
	
	public void printJobs(String queueName, int max) {
		int i = 0;
		Message msg = null;
		if (!queueName.equals("all")) {
			while (i<max&&(msg=sqsUtils.nextMessage(10, queueName))!=null) {
					System.out.println(msg.toString());
					i++;
			}
		} else {
			for (String q : conf.getQueues()) {
				System.out.println("Deleting queue:"+q);
				while (i<max&&(msg=sqsUtils.nextMessage(10, q))!=null) {
					System.out.println(msg.toString());
					i++;
			}
			}
		}
		System.out.println("Printed "+i+" messages");
	}
	
	public void deleteJobs(String queueName) {
		if (!queueName.equals("all")) {
			System.out.println("Deleted: "+sqsUtils.clearQueue(queueName)+" jobs");
		} else {
			for (String q : conf.getQueues()) {
				System.out.println("Deleting queue:"+q);
				System.out.println("Deleted: "+sqsUtils.clearQueue(q)+" jobs");
			}
		}
	}
	
	@SuppressWarnings("static-access")
	public static void main(String args[]) {
		HelpFormatter formatter = new HelpFormatter();
		Options options = new Options();
		MessageControl mc = null;
		try {
			if (args.length>0) {
				
				options.addOption(OptionBuilder.withArgName("config").isRequired(true).hasArg().withDescription("xml file with configuration").create("f"));
				options.addOption(OptionBuilder.withArgName("command").isRequired(true).hasArg().withDescription("command: [clear,print]").create("mode"));
				options.addOption(OptionBuilder.withArgName("queue").isRequired(true).hasArg().withDescription("queue: queuename").create("queue"));
				options.addOption(OptionBuilder.withArgName("maxJobs").isRequired(false).hasArg().withDescription("maxJobs: the maximum number of jobs to print").create("maxJobs"));
				
				options.addOption("help",false,"this message");
				CommandLineParser parser = new GnuParser();
				CommandLine cmd = parser.parse( options,args);
				String file = cmd.getOptionValue("f");
				String mode = cmd.getOptionValue("mode");
				Configuration conf = Configuration.getConfiguration(file);
				String queue = cmd.getOptionValue("queue");
				mc = new MessageControl(new SQSUtils(conf),conf);
				if (cmd.hasOption("help")) {
					formatter.printHelp("MessageControl",options);
					System.exit(0);
				}
				if (mode.equals("print")) {
					System.out.println();
					int max = 1000;
					if (cmd.hasOption("maxJobs")) { 
						max = Integer.parseInt(cmd.getOptionValue("maxJobs"));
					}
					
					mc.printJobs(queue,max);
				}	else if(mode.equals("clear")) {
					
					mc.deleteJobs(queue);
				} else {
					System.out.println("Error, command not understood.");
					formatter.printHelp("MessageControl",options);
					System.exit(1);
				}
	    } else {
	    	formatter.printHelp("MessageControl",options);
	    }
		} catch (ParseException pe) {
			 System.err.println( "Invalid parameters.  Reason: " + pe.getMessage() );
			 formatter.printHelp("MessageControl",options);
			 System.exit(1);
		}
	}
	
	
}
